﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class EditionController : BaseController
    {
        #region 获取列表
        [Description("版本号列表")]
        [ViewPage]
        public ActionResult EditionList()
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var querylist = dbcontext.ADZ_APPUpGradeInfo.ToList();
            var rowcounts = querylist.Count;

            PagedList<ADZ_APPUpGradeInfo> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_APPUpGradeInfo>(querylist, 1, 100, rowcounts);
            return View(pagedlist);
        }

        #endregion

        [Description("版本管理")]
        [ViewPage]
        public ActionResult EditEdition(long id)
        {
            ViewBag.Title = "版本管理";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_APPUpGradeInfo paramModel = new ADZ_APPUpGradeInfo();
            if (id > 0)
            {
                paramModel = dbcontext.ADZ_APPUpGradeInfo.FirstOrDefault(t => t.ID == id);

            }

            return View(paramModel);
        }

        [Description("版本管理")]
        [HttpPost]
        public ActionResult EditEdition(ADZ_APPUpGradeInfo paramModel)
        {
            Utils.Trim(paramModel);
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            if (paramModel.SystemType == 1)
            {
                ///判断是否有文件上传
                HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                if (hfc != null && hfc.Count > 0)
                {
                    for (int i = 0; i < hfc.Count; i++)
                    {
                        var file = hfc[i];
                        if (file == null || string.IsNullOrWhiteSpace(file.FileName))
                            continue;
                        try
                        {
                            var putResult = ApkFileBiz.UpFile(file);
                            if (!putResult.Item1)
                            {
                                resMsg.Message += (putResult.Item2);
                                return Json(resMsg, JsonRequestBehavior.AllowGet);
                            }
                            else
                            {
                                paramModel.DowloadAddress = putResult.Item3;
                            }
                        }
                        catch (Exception ex)
                        {
                            resMsg.Message = ($"文件上传_{file.FileName}异常:" + ex.GetExcetionMessage());
                            return Json(resMsg, JsonRequestBehavior.AllowGet);
                        }
                    }
                }
            }


            var entity = dbcontext.ADZ_APPUpGradeInfo.FirstOrDefault(t => t.ID == paramModel.ID);
            if (entity != null)
            {
                List<string> exclist = new List<string>();
                exclist.Add("ID");
                exclist.Add("CreateTime");

                Utils.Mapper<ADZ_APPUpGradeInfo, ADZ_APPUpGradeInfo>(entity, paramModel, exclist);
                entity.UpdateTime = DateTime.Now;

                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}